/**
 ******************************************************************************
 * @file	u_math.c
 * @brief	部分精简的C库


- 2013-12-11,MenglongWu,MenglongWoo@aliyun.com

 * Copyright (C) 2017 MenglongWu <MenglongWoo@aliyun.com>
*/

// 10^0    ~ 10^9    以  1 步进
volatile const float c10_1_0[10]   = {1,        10,      100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
// 10^0.0  ~ 10^0.9  以0.1步进
volatile const float c10_0_1[10]    = {1.000000 , 1.258925 , 1.584893 , 1.995262 , 2.511886 , 3.162278 , 3.981072 , 5.011872 , 6.309573 , 7.943282};
// 10^0.00 ~ 10^0.09 以0.01步进
volatile const float c10_0_01[10]   = {1.000000 , 1.023293 , 1.047129 , 1.071519 , 1.096478 , 1.122018 , 1.148154 , 1.174898 , 1.202264 , 1.230269 };
volatile const float c10_0_001[10]  = {1.000000 , 1.002305 , 1.004616 , 1.006932 , 1.009253 , 1.011579 , 1.013911 , 1.016249 , 1.018591 , 1.020939 };
volatile const float c10_0_0001[10] = {1.000000 , 1.000230 , 1.000461 , 1.000691 , 1.000921 , 1.001152 , 1.001383 , 1.001613 , 1.001844 , 1.002074 };

/**
 * @brief	指数计算精简版，只能计算10为底的指数，指数范围从 9.9999 ~ -9.9999
 相比于 C库pow 编译进去要小 2440 Byte
 * @param	base     底数（无任何意义，只是为了与 C库 pow(a,b)兼容
 * @param	exponent 指数值
 * @retval	null
 */
float _pow(int base, float exponent)
{
	int    a1, a2, a3, a4;
	int    b1 = 0;
	int    flag = 0;
	float  tmp;
	

	if(exponent < 0) {
		exponent = -exponent;
		flag = 1;
	}
	if(exponent >= 10) {
		return 0;
	}

	// 如果  exponent 为 4.7132
	// 提取小数前 第1位
	b1 = (int)exponent;		// OK   b1 = 4
	exponent = exponent - b1;	//      ex = 0.7132


	// 提取小数
	exponent *= 10;			//      ex = 7.132
	a1 = (int)exponent;		// OK   a1 = 7
	exponent *= 10;			//      ex = 71.32
	a2 = (int)exponent;		//      a2 = 71
	exponent *= 10;			//      ex = 713.2
	a3 = (int)exponent;		//      a3 = 713
	exponent *= 10;			//      ex = 7132
	a4 = (int)exponent;		//      a4 = 7132
	// exponent *= 10;

	a4 = a4 - a3 * 10;		// OK   a4 = 2 = 7132 - 713 * 10
	a3 = a3 - a2 * 10;		// OK   a3 = 3 = 713  - 71  * 10
	a2 = a2 - a1 * 10;		// OK   a2 = 1 = 71   - 7   * 10

	tmp = (float)(
	          c10_0_1[a1]    *
	          c10_0_01[a2]   *
	          c10_0_001[a3]  *
	          c10_0_0001[a4] *
	          c10_1_0[b1]);

	// 如果 exponent 是负数则取倒数
	if(flag) {
		tmp = 1 / tmp;
	}
	return tmp;
}

double _log(double a)  
{  
   int N = 15;//我们取了前15+1项来估算  
   int k,nk;  
   double x,xx,y;  
   x = (a-1)/(a+1);  
   xx = x*x;  
   nk = 2*N+1;  
   y = 1.0/nk;  
   for(k=N;k>0;k--)  
   {  
     nk = nk - 2;  
     y = 1.0/nk+xx*y;  
       
   }  
   return 2.0*x*y;  
     
}  